#include "../proto/codec.h"
#include "../proto/dispatcher.h"

#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"

#include "request.pb.h"
#include <iostream>
class Server
{
    typedef std::shared_ptr<bit::translaterequest> translaterequestPtr;
    typedef std::shared_ptr<bit::translateresponse> translateresponsePtr;
    typedef std::shared_ptr<bit::calculationrequest> calculationrequestPtr;
    typedef std::shared_ptr<bit::calculationresponse> calculationresponsePtr;

public:
    Server(int port) : _server(&_loop, muduo::net::InetAddress("0.0.0.0", port), "Server", muduo::net::TcpServer::kReusePort),
                       _dispatcher(std::bind(&Server::onUnknownMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
                       _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        _dispatcher.registerMessageCallback<bit::translaterequest>(
            std::bind(&Server::onTranslate, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<bit::calculationrequest>(
            std::bind(&Server::oncalculation, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec,
                                             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _server.setConnectionCallback(std::bind(&Server::onConnection, this, std::placeholders::_1));
    }
    void Start()
    {
        _server.start();
        _loop.loop();
    }
    void onUnknownMessage(const muduo::net::TcpConnectionPtr &conn,
                          const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }
    std::string translate(const std::string &str)
    {
        static std::unordered_map<std::string, std::string> dict_map = {
            {"hello", "你好"},
            {"Hello", "你好"},
            {"你好", "Hello"},
            {"吃了吗", "油泼面"}};
        auto it = dict_map.find(str);
        if (it == dict_map.end())
        {
            return "没听懂！！";
        }
        return it->second;
    }
    void onTranslate(const muduo::net::TcpConnectionPtr &conn,
                     const translaterequestPtr &message,
                     muduo::Timestamp)
    {
        // 1. 提取message中的有效消息，也就是需要翻译的内容
        std::string req_msg = message->msg();
        // 2. 进行翻译，得到结果
        std::string rsp_msg = translate(req_msg);
        // 3. 组织protobuf的响应
        bit::translateresponse resp;
        resp.set_msq(rsp_msg);
        // 4. 发送响应
        _codec.send(conn, resp);
    }
    void oncalculation(const muduo::net::TcpConnectionPtr &conn,
                       const calculationrequestPtr &message,
                       muduo::Timestamp)
    {
        int num1 = message->num1();
        int num2 = message->num2();
        int result = num1 + num2;
        bit::calculationresponse resp;
        resp.set_result(result);
        _codec.send(conn, resp);
    }
    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if (conn->connected())
        {
            LOG_INFO << "新连接建立成功！";
        }
        else
        {
            LOG_INFO << "连接即将关闭！";
        }
    }

private:
    muduo::net::EventLoop _loop;
    muduo::net::TcpServer _server;  // 服务器
    ProtobufDispatcher _dispatcher; // 请求分发器对象--要向其注册处理函数
    ProtobufCodec _codec;           // protobuf协议处理器---针对收到的数据进行对应的协议处理
};
int main()
{
    Server s(8888);
    s.Start();
    return 0;
}